<template>
  <div class="login">
    <div class="logo"></div>
    <div class="account">
      <div class="accountLogin">
        <div class="accountInfo">
          <div class="userDate">
            <h3>让学生做好自己</h3>
            <h6>大数据查询 / 生涯管理 / 测评 / 志愿填报</h6>
            <el-tabs
              v-model="activeName"
              style="text-align: center; margin-bottom: 10px"
              @tab-click="handleClick"
            >
              <el-tab-pane label="密码登录" name="first"></el-tab-pane>
              <el-tab-pane label="验证码登录" name="tree"></el-tab-pane>
            </el-tabs>
            <el-form
              ref="ruleFormRef"
              style="max-width: 600px"
              :model="ruleForm"
              :rules="loginRules"
              label-width="auto"
              class="demo-ruleForm"
              :label-position="'top'"
              status-icon
            >
              <template v-if="activeName == 'first'">
                <el-form-item label="用户名" prop="mobile" label-position="top">
                  <el-input
                    v-model="ruleForm.mobile"
                    class="height40"
                    placeholder="请输入手机号"
                  />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input
                    v-model="ruleForm.password"
                    class="height40"
                    placeholder="请输入密码"
                    type="password"
                    show-password
                  />
                </el-form-item>
              </template>
              <template v-else>
                <el-form-item label="手机号" prop="mobile">
                  <el-input
                    v-model="ruleForm.mobile"
                    class="height40"
                    placeholder="请输入手机号"
                  />
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                  <el-input
                    v-model="ruleForm.code"
                    class="height40"
                    placeholder="请输入验证码"
                  >
                    <template #suffix>
                      <div
                        :class="isDisabled == false ? 'codename' : 'codeInfo'"
                        @click="resetchange"
                      >
                        {{ buttonName }}
                      </div>
                    </template>
                  </el-input>
                </el-form-item>
              </template>
              <el-form-item>
                <el-button
                  type="primary"
                  class="height40 matop"
                  style="width: 100%"
                  @click="submitForm(ruleFormRef)"
                >
                  登录
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import useUserStore from '@/store/modules/user';
import { useRouter, useRoute } from 'vue-router';
import { sendCode } from '@/api/login.js';

import { ElMessage } from 'element-plus';

const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();
const activeName = ref('first');

const redirect = ref(undefined);
watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
  },
  { immediate: true }
);
// 发送验证码
const buttonName = ref('发送验证码');
const isDisabled = ref(false);
const time = ref(60);

const resetchange = async () => {
  if (!ruleForm.mobile) {
    ElMessage.error('请输入手机号码');
    return;
  }

  if (isDisabled.value === true) return;

  isDisabled.value = true; // 禁用按钮
  let interval = window.setInterval(function () {
    buttonName.value = time.value + '秒后重发';
    --time.value;
    if (time.value < 0) {
      buttonName.value = '重新发送';
      time.value = 60;
      isDisabled.value = false;
      window.clearInterval(interval); // 清除计时器
    }
  }, 1000);
  try {
    await sendCode({ phone: ruleForm.mobile });
  } catch (error) {
    console.log('error:', error);
    resetButtonState(interval); //  调用重置按钮状态的函数
  }
};
// 抽取出重置按钮状态的函数
const resetButtonState = (interval) => {
  proxy.$refs.ruleFormRef.clearValidate();
  buttonName.value = '发送验证码'; // 恢复按钮文字
  time.value = 60; // 重置倒计时
  isDisabled.value = false; // 重新启用按钮
  window.clearInterval(interval); // 停止倒计时
};

// 切换状态
function handleClick(val) {
  if (val.index == '1') {
    ruleForm.grant_type = 'sms';
    ruleForm.mobile = '';
    ruleForm.password = '';
    ruleForm.code = '';
  } else {
    ruleForm.grant_type = 'password';
    ruleForm.mobile = '';
    ruleForm.password = '';
  }
}

// 登录功能
const ruleFormRef = ref(null);

const ruleForm = reactive({
  grant_type: 'password', // 登录类型
  mobile: '', // 手机号
  password: '', // 密码
  code: '', // 验证码
});

const loginRules = {
  mobile: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
  code: [{ required: true, trigger: 'blur', message: '请输入验证码' }],
};
function submitForm() {
  proxy.$refs.ruleFormRef.validate((valid) => {
    if (valid) {
      userStore.login(ruleForm).then(() => {
        router.push({ path: '/index' });
      });
    }
  });
}
</script>

<style lang="scss" scoped>
:deep(.el-tabs__nav-scroll) {
  display: flex;
  justify-content: space-around;
}

.login {
  height: 100%;
  background-image: url('../assets/images/login-background.png');
  background-size: cover;
  overflow: hidden;
  display: flex;
  flex-direction: row;
}

.logo {
  display: inline-block;
  position: absolute;
  width: 40%;
  left: -10px;
  height: calc(100% - 10vh);
  background-image: url('../assets/images/logo.png');
  background-size: 90% 90%;
  background-repeat: no-repeat;
  //background-size: cover;
  margin-top: 60px;
  z-index: 2;
}

.account {
  display: inline-block;
  position: absolute;
  height: 572px;
  top: 14.86%;
  right: 10%;
  z-index: 3;

  //background-color: orange;
  .accountLogin {
    margin-left: 866px;
    display: inline-block;
    width: calc(100% - 860px);
    height: 572px;
    text-align: right;

    //background-color: #004cf7;
    .accountInfo {
      display: inline-block;
      width: 460px;
      height: 572px;
      margin-right: 30%;
      background-color: rgb(255, 255, 255);
      border-radius: 20px;

      .userDate {
        width: 320px;
        height: auto;
        margin: 0 auto;
        padding-top: 50px;

        .codename {
          font-weight: 600;
          color: rgb(75, 144, 247);
          line-height: 16px;
          height: 16px;
          font-size: 16px;
          text-align: center;
          cursor: pointer;
        }

        .codeInfo {
          font-weight: 600;
          color: rgb(153, 153, 153);
          line-height: 16px;
          height: 16px;
          font-size: 16px;
          text-align: center;
          cursor: pointer;
        }

        h3 {
          font-weight: 600;
          font-size: 34px;
          color: #0f5cfb;
          text-align: center;
          margin: 0;
          padding: 0;
          margin-bottom: 16px;
        }

        h6 {
          color: #0f5cfb;
          font-size: 16px;
          margin-bottom: 16px !important;
        }
      }
    }
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
:deep(.el-form-item--label-top) {
  text-align: left;
}
</style>
